<template>
  <div class="container">
    <div class="container-title">
      <p>商家类型</p>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>社区生活</el-breadcrumb-item>
        <el-breadcrumb-item>周边商家</el-breadcrumb-item>
        <el-breadcrumb-item>商家类型</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div class="search-box">
      <el-form ref="form" :model="form">
        <el-form-item>
          <!-- 查询框 -->
          <el-input v-model="typeName" placeholder="优惠券名称" clearable></el-input>

          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-form-item>
        <div class="block"></div>
        <el-form-item>
          <el-button
            type="success"
            @click="onSearch"
            icon="el-icon-search"
            style="background-color: #0bb197"
            >查询</el-button
          ><el-button @click="onReset" icon="el-icon-refresh" class="reset"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <!-- 表格 -->
    <div class="data-list">
      <div class="data-list-title">
        <span>查询结果列表</span>
        <el-button
          type="success"
          style="background-color: #0bb197"
          @click="addDialog = true"
          >新增</el-button
        >
      </div>

      <!-- 新增弹出框 -->
      <el-dialog title="新增类型" :visible="addDialog" class="dines">
        <el-form :model="addData">
          <el-form-item
            label="*类型名称"
            :label-width="formLabelWidth"
            class="name"
          >
            <el-input v-model="addData.typeName" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div class="dialog-footer" align="center">
          <el-button @click="addDialog = false" class="off">取 消</el-button>
          <el-button type="primary" @click="add" class="on">确 定</el-button>
        </div>
      </el-dialog>

      <!-- 编辑弹出框 -->
      <el-dialog title="修改类型" :visible="redDia" class="dines">
        <el-form :model="redData">
          <el-form-item label="*类型名称" class="name">
            <el-input v-model="redData.typeName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="*创建时间" class="name">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="redData.createTime"
              style="width: 100%"
            ></el-date-picker>
          </el-form-item>
        </el-form>
        <div class="dialog-footer" align="center">
          <el-button @click="redDia = false" class="off">取 消</el-button>
          <el-button type="primary" @click="edit" class="on"
            >确 定</el-button
          >
        </div>
      </el-dialog>

      <!-- 表单内容 -->

      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column
          prop="merchantId"
          label="序号"
          width="180"
          align="center"
        ></el-table-column>
        <el-table-column prop="typeName" label="类型名称" align="center">
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" align="center">
          <template slot-scope="scope">
            <span>{{ timeTrans(scope.row.createTime, 1) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button @click="handleEdit(scope.row)" type="text" size="small"
              >编辑</el-button
            >
            <el-button
              size="small"
              type="text"
              @click="deleteRow(scope.row.merchantId)"
              >删除</el-button
            >
          </template></el-table-column
        >
      </el-table>
      <!-- 页码 -->
      <el-pagination
        class="msg-pagination-container"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage4"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="count"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :background="true"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import time from "../utils/time";
export default {
  mixins: [time],
  data: function () {
    return {
      currentPage4: 1,
      addDialog: false,
      redDia: false,
      typeName: "",
      page: 1,
      count: 5,
      total: 1,

      addData: {
        typeName: "",
      },
      redData: {},
      options: [
        // {
        //   value: '0',
        //   label: '已完成',
        // },
        // {
        //   value: '1',
        //   label: '发布中',
        // },
      ],
      form: { status: "", date: "" },
      tableData: [],
    };
  },
  methods: {
    // 查询
    onSearch: function () {
      this.getData();
    },

    // 新增
    add: function () {
      this.$axios({
        url: "/smart/merchant-type/add",
        method: "POST",
        data: this.addData,
      }).then(({ data }) => {
        if (data.code == "00000") {
          this.$message.success("添加成功");
          this.addDialog = false;
          this.getData();
        } else {
          this.$message.error("添加失败");
        }
      });
    },

    // 删除
    deleteRow(row) {
      console.log(row);
      this.$axios({
        url: "/smart/merchant-type/delete/" + row,
        method: "DELETE",
        
      }).then(({ data }) => {
        if (data.code == "00000") {
          this.$message.success("删除成功");
          this.getData();
        } else {
          this.$message.error("删除失败");
        }
      });
    },

    //赋值
    handleEdit(row) {
      this.redDia = true;
      let redData = { ...row };
      redData.checkinTime = this.timeTrans(redData.createTime, 1);
      this.redData = redData;
    },

    // 编辑
    edit: function (row) {
      // this.merchantId = row;
      this.$axios({
        url: "/smart/merchant-type/edit",
        method: "put",
        data: this.redData,
      }).then(({ data }) => {
        console.log(data);
        if (data.code == 0) {
          this.$message.success("修改成功");
          this.redDia = false;
          this.getData();
        } else {
          this.$message.error("修改失败");
        }
      });
    },

    onReset: function () {
      this.typeName = "";
      this.getData();
      
    },
    detail: function (row) {
      // console.log(row, detail);
    },
    finish: function (row) {
      // console.log(row, finish);
    },
    handleSizeChange(val) {
      this.count = val;
      this.getData();
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getData();
      // console.log(`当前页: ${val}`);
    },

    // 数据链接
    getData: function () {
      this.$axios({
        url: "/smart/merchant-type/search",
        method: "post",
        params:{
          pageNum:this.page,
          pageSize:this.count,
          typeName:this.typeName,
        },
      }).then(({ data }) => {
        console.log(data);
        if (data.code == "00000") {
          this.tableData = data.data.pageInfo.list;
          this.total = data.data.pageInfo.total;
        } else {
          this.$message.error("数据加载失败！");
        }
      });
    },
  },
  mounted: function () {
    this.getData();
  },
};
</script>

<style lang="less" scoped>
.container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  .container-title {
    width: 98%;
    height: 60px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    p {
      color: #333333;
      font-size: 16px;
      font-weight: 400;
      font-family: "Arial Normal", "Arial";
    }
    .el-breadcrumb-item {
      color: rgba(148, 148, 148, 0.898039215686275);
      font-size: 12px;
      font-weight: 400;
      font-family: "Arial Normal", "Arial";
    }
  }
  .search-box {
    width: 98%;
    height: 80px;
    background: #ffffff;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    border: 1px solid #ffffff;
    border-radius: 5px;
    .el-form {
      width: 42%;
      background: #ffffff;
      display: flex;
      justify-content: space-around;
      align-items: center;
      .el-form-item {
        margin-bottom: 0;
        .el-select {
          width: 155px;
        }
        .reset {
          background-color: white;
          color: #c0c4cc;
          border: 1px solid #c0c4cc;
        }
        .reset:hover {
          color: #0bb197;
        }
      }
      .el-date-editor--datetimerange.el-input__inner {
        width: 310px;
      }
    }
  }
  .data-list {
    width: 98%;
    height: 100%;
    background: #ffffff;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-around;
    margin-top: 20px;
    .data-list-title {
      width: 98%;
      height: 60px;
      display: flex;
      justify-content: space-between;
      align-items: center;

      span {
        height: 16px;
        font-family: "Arial Normal", "Arial";
        font-weight: 700;
        font-style: normal;
        font-size: 13px;
        color: #3c4145;
      }
    }

    .dialog-footer {
      width: 400px;
      .off {
        background-color: white;
        color: #c0c4cc;
        border: 1px solid #c0c4cc;
      }
      .off:hover {
        color: #0bb197;
      }
      .on {
        background-color: #0bb197;
      }
    }

    .dines {
      width: 900px;
      left: 50%;
      margin-left: -450px;
    }
    .name {
      width: 350px;
    }
    .el-table {
      flex: 1;
      .el-button {
        color: #0bb197;
      }
    }
    .el-pagination {
      width: 98%;
      height: 60px;
      display: flex;
      justify-content: flex-end;
      align-items: center;
    }
  }
}
</style>
<style>
.el-pagination.is-background .el-pager li:not(.disabled):hover {
  color: #0bb197;
}
.el-pagination.is-background .el-pager li:not(.disabled).active {
  background-color: #0bb197;
  color: #fff;
}
</style>
